<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/6/21
 * Time: 16:50
 */

namespace app\common\model;

use think\Cache;

/**
 * 准了货品相关模型
 * Class ZhunleGoodsCommodity
 * @package app\common\model
 */
class ZhunleGoodsCommodity extends Base
{
    protected $table = 'wechat_backup_07_18_14_43_00.zhunle_goods_commodity';

    const GOODS_COMMODITY = 'goods:commodity:';
    const COMMODITY_INFO = 'commodity:info:';
    const CACHE_TIME = 86400 * 7;

    /**
     * 获取当前商品上架的所有货品信息
     * @param array $map
     * @return bool|mixed
     */
    public function getGoodsCommodity($map = [])
    {
        $gid = $map['gid'];

        $commids = false;
        $data_list = [];

        if ($this->cache_on >= 1) {
            $commids = Cache::get(self::GOODS_COMMODITY . $gid);
        }

        if ($commids === false) {
            try {
                $data = $this->where(['gid' => $gid, 'status' => 1])->column('id');
                if ($data) {
                    $commids = implode(',', $commids);
                }
                if ($commids && $this->cache_on >= 1) Cache::set(self::GOODS_COMMODITY . $gid, $data, self::CACHE_TIME);
            } catch (\Exception $e) {
                $this->error = '获取货品信息失败.';
                return false;
            }

            $commids = explode(',', $commids);

            if (!empty($commids)) {
                $ac_ids = '';
                $temp = [];

                foreach ($commids as $ac_id) {
                    if ($this->cache_on >= 1) {
                        $temp[$ac_id] = Cache::get(self::COMMODITY_INFO . $ac_id);
                    } else {
                        $temp[$ac_id] = false;
                    }
                    if ($temp[$ac_id] === false && $ac_id) {
                        $ac_ids .= $ac_id . ',';
                    }
                }

                if ($ac_ids !== '' || strlen($ac_ids) > 0) {
                    //去掉结尾的 ，
                    $ac_ids = substr($ac_ids, 0, strlen($ac_ids) - 1);

                    try {
                        $where = ['status' => 1];
                        $data = $this->where($where)->where('id in (' . $ac_ids . ')')->order('sort', 'DESC')->select();
                        $data = json_decode(json_encode($data, true), true);
                    } catch (\Exception $e) {
                        $this->error = '获取货品信息失败';
                        return false;
                    }

                    if (!empty($data)) {
                        foreach ($data as $c) {
                            if ($this->cache_on >= 1) {
                                Cache::set(self::COMMODITY_INFO . $c['id'], $c, self::CACHE_TIME);
                            }

                            if ($c) {
                                $temp[$c['id']] = $c;
                            }
                        }
                    }

                    if (!empty($temp)) {
                        foreach ($data as $item) {
                            $data_list[] = $item;
                        }
                    }
                }
            }
        }

        return $data_list;
    }

    /**
     * 获取具体货品的信息
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function getCommodityInfo($map = [])
    {
        $id = $map['id'];

        $data = false;

        if ($this->cache_on >= 1) {
            $data = Cache::get(self::COMMODITY_INFO . $id);
        }
        if ($data === false) {
            $where = ['id' => $id, 'status' => 1];

            try {
                $data = $this->where($where)->findOrFail();
                $data = json_decode(json_encode($data, true), true);
                if ($data) {
                    Cache::set(self::COMMODITY_INFO . $id, $data, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                $this->error = '获取货品信息失败';
                return false;
            }
        }

        return $data;
    }
}
